<template>
  <div class="login-container">
    <!-- 导航栏区域 -->
    <van-nav-bar title="标题" class="navbar" />

    <!-- 登录表单区域 -->
    <!-- 给表单数据添加验证验证不通过，不会发起请求 -->
    <van-form @submit="onSubmit" ref="loginRef">
      <van-field
        v-model="user.mobile"
        name="mobile"
        placeholder="请输入手机号"
        :rules="userFormRules.mobile"
        type="number"
        maxlength="11"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        v-model="user.code"
        name="code"
        placeholder="请输入验证码"
        :rules="userFormRules.code"
        type="number"
        maxlength="6"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <!-- 倒计时按钮 -->
          <van-count-down
            v-if="isCountShow"
            :time="5000"
            format="ss s"
            @finish="isCountShow=false"
          />
          <van-button
            v-else
            round
            size="small"
            type="info"
            plain
            class="sendBtn"
            native-type="button"
            @click="sendMssBtn"
          >发送验证码</van-button>
        </template>
      </van-field>
      <div class="login-btn-box">
        <van-button block type="info" native-type="submit" class="login-btn">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendSms } from '@/api/user.js'
export default {
  name: 'login',
  data() {
    return {
      user: {
        mobile: '',
        code: ''
      },
      // 登陆表单验证对象
      userFormRules: {
        mobile: [
          { required: true, message: '请填写手机号' },
          { pattern: /^1[3|5|7|8]\d{9}$/, message: '请检查手机号是否正确' }
        ],
        code: [
          { required: true, message: '请填写验证码' },
          { pattern: /^\d{6}$/, message: '请检查验证码是否正确' }
        ]
      },
      // 是否展示倒计时按钮
      isCountShow: false
    }
  },
  methods: {
    async onSubmit() {
      // 自定义加载图标
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        loadingType: 'spinner',
        duration: '0' // 设置为0的目的就是让其一直加载提示
      })
      try {
        const { data } = await login(this.user)
        this.$store.commit('setUser', data.data)
        this.$toast.success('登录成功')

        this.$router.back()
      } catch (error) {
        if (error.response.status === 400) {
          this.$toast.fail('请输入正确的手机号，验证码')
        } else {
          this.$toast.fail('登录失败')
        }
      }
    },

    // 验证手机号
    async sendMssBtn() {
      // 1、验证手机号是否输入
      try {
        await this.$refs.loginRef.validate('mobile')
        console.log('验证成功')
      } catch (error) {
        // 验证失败，不向下执行（return）
        return console.log('验证没有通过', error)
      }

      // 倒计时效果
      this.isCountShow = true
      // 发送验证码操作
      try {
        await sendSms(this.user.mobile)
        this.$toast('获取验证码成功')
      } catch (error) {
        if (error.response.status === 429) {
          this.$toast('发送验证码太过频繁')
        } else {
          this.$toast('获取验证码失败')
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  .toutiao {
    font-size: 37px;
  }
  .van-field__control {
    background-color: #fff;
  }
  .sendBtn {
    width: 150px;
    height: 44px;
    font-size: 21px;
    line-height: 44px;
    color: #666;
    background-color: #ededed;
  }
  .login-btn-box {
    padding: 51px 28px;
    .login-btn {
      background-color: #6db4fb;
      height: 90px;
      border: none;
    }
  }
}
</style>
